//
//  WCQuestionnaireVC.m
//  WC
//
//  Created by wsj2015 on 16/1/26.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCQuestionnaireVC.h"
#import "WCQuestionListCell.h"
#import "WCQuestionnaireDetailVC.h"
#import "WCInfoEditView.h"
#import "WCNSendQuestionVC.h"

@interface WCQuestionnaireVC ()<UIAlertViewDelegate>


@property (nonatomic,  retain) NSMutableArray *dataArray;

@end

@implementation WCQuestionnaireVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    [self.tableView.header beginRefreshing];
}
-(NSMutableArray *)dataArray{
    if (_dataArray ==nil) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;

}
- (void)configUI{
   
    
    self.title = @"问卷记录";
    self.view.backgroundColor =kAppBg;
    
    [self addRightBarButtonItemWithTitle:@"发送问卷" target:self action:@selector(sendQuestionnaireAction:)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WCQuestionListCell" bundle:nil] forCellReuseIdentifier:@"WCQuestionListCellId"];
    defineWeakSelf;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf postQestionnareList];
    }];
    
    
    
    
}
#pragma mark -aciton
- (void)sendQuestionnaireAction:(id)sender{
    defineWeakSelf;
    WCNSendQuestionVC *vc = [WCNSendQuestionVC new];
    vc.recordId = self.recordId;
    vc.isAutoRefresh = ^(){
        [weakSelf.tableView.header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -请问卷列表数据
- (void)postQestionnareList{
     defineWeakSelf;
    [WCPopupLayerManager showLodingView];

    NSDictionary *param  =@{@"recordId":@(self.recordId),@"nurseId":@([[WCDefaultManager shareMg] nurseId]),@"qflag":@(1)};

    [[WCNetworkAccessManager sharedMg] POST:kQuestionnaireRecordList parameters:param success:^(NSDictionary *responseObject) {
        [weakSelf.dataArray removeAllObjects];
        for (NSDictionary *dict in responseObject[@"data"]) {
            WCQestionnaireModel *mdel =[WCQestionnaireModel new];
            [mdel setValuesForKeysWithDictionary:dict];
            [weakSelf.dataArray addObject:mdel];
        }
        [weakSelf.tableView reloadData];
        
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
        [weakSelf.tableView.header endRefreshing];
    }];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==1) {
//        alertView.t
        
    }

}

#pragma mark -提交反馈
- (void)commitFeedBackToPatient:(NSString *)feedBack withQriId:(NSInteger )qriId{
    [WCPopupLayerManager showLodingView];
    weakSelf_define;
//    qriId : 问卷记录ID(必填)  feedback : 反馈信息
    NSDictionary *params =@{@"qriId":@(qriId),@"feedback":feedBack};
    [[WCNetworkAccessManager sharedMg] POST:kSaveFeedBack parameters:params success:^(NSDictionary * responseObject) {
        CoreSVPSuccess(responseObject[@"data"]);
        [weakSelf.tableView.header beginRefreshing];
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WCQuestionListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCQuestionListCellId" forIndexPath:indexPath];
    WCQestionnaireModel *model =self.dataArray[indexPath.row];
    cell.model =model;
    cell.chekAction = ^(){
        NSString *feedBackContent;
        NSString *alertTitle;
        UIAlertViewStyle alertStyle;
        NSString *cancelTitle;
        NSString *confirmTitle;
        if (model.feedback) {
            feedBackContent  =model.feedback;
            alertTitle =@"我的反馈内容";
            cancelTitle =@"知道了";
            alertStyle =0;
            confirmTitle = nil;
            UIAlertView *alerView =[[UIAlertView alloc] initWithTitle:alertTitle message:feedBackContent delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:confirmTitle,nil];
            //         护士端可以输入
            alerView.alertViewStyle = alertStyle;
            [alerView show];
        }else{
            weakSelf_define;
            WCInfoEditView *editView =[[WCInfoEditView alloc] init];
            editView.type = WCInfoEditTypeText;
            editView.titleLabel.text =@"请给用户填写反馈";
            editView.save = ^(NSString *tex){
                if ([tex isEqualToString:@""]) {
                    return NO;
                }else{
                    [weakSelf commitFeedBackToPatient:tex withQriId:[model.qriId integerValue]];
                    
                    return YES;
                    
                }
            };
            [editView showIn:weakSelf.view animated:YES];
        }
        
        
        
    };
    
    
    return  cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WCQestionnaireModel *model =self.dataArray[indexPath.row];
    WCQuestionnaireDetailVC *vc =[WCQuestionnaireDetailVC new];
    
    //拼接参数
//    opFlag=0，
//    qriId=4，
//    questionId=4
//    int opFlag =[model.isOver integerValue]==0?1:0;
    int opFlag = 0;
    NSInteger qriId =[model.qriId integerValue];
    NSInteger questionId =[model.questionId integerValue];
    NSString *nurStr =[NSString stringWithFormat:@"%@?opFlag=%i&qriId=%i&questionId=%i",model.qHttpUrl,opFlag,qriId,questionId];
     NSString * urlStrCode = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)nurStr,NULL,NULL,kCFStringEncodingUTF8));
    vc.url =[NSURL URLWithString:urlStrCode];
    [self.navigationController pushViewController:vc animated:YES];
    
}




@end
